﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ElectronicCommerce.Framework.Payment.Contract.DataContract;
using ElectronicCommerce.Framework.Payment.Data.Interfaces;

namespace ElectronicCommerce.Framework.Payment.Data
{
    public static class PaymentLogger
    {
        /// <summary>
        /// 记录支付请求日志
        /// </summary>
        /// <param name="?"></param>
        public static void LogPaymentRequest(PaymentRequest paymentRequest)
        {
            if (paymentRequest is ChargeRequest)
            {
                LogChargeRequest((ChargeRequest)paymentRequest);
            }
        }

        /// <summary>
        /// 记录支付响应日志
        /// </summary>
        /// <param name="paymentResult"></param>
        public static void LogPaymentResponse(PaymentResult paymentResult)
        {
            if (paymentResult is ChargeCallbackResult)
            {
                LogChargeCallbackResult((ChargeCallbackResult)paymentResult);
            }
        }

        /// <summary>
        /// 记录退款请求
        /// </summary>
        /// <param name="request"></param>
        /// <param name="result"></param>
        public static void LogRefundHistory(RefundRequest request, RefundResult result)
        {
            IRefundHistoryRepository refundHistoryRepository =
                RepositoryFactory.CreateRefundHistoryRepository();
            refundHistoryRepository.InsertRefundHistory(request, result);
        }

        /// <summary>
        /// 记录收费请求
        /// </summary>
        /// <param name="chargeRequest"></param>
        private static void LogChargeRequest(ChargeRequest chargeRequest)
        {
            IChargeHistoryRepository chargeHistoryRepository = RepositoryFactory.CreateChargeHistoryRepository();
            chargeHistoryRepository.InsertChargeRequestHistory(chargeRequest);
        }

        /// <summary>
        /// 记录支付响应日志
        /// </summary>
        /// <param name="callbackResult"></param>
        private static void LogChargeCallbackResult(ChargeCallbackResult callbackResult)
        {
            IChargeHistoryRepository chargeHistoryRepository = RepositoryFactory.CreateChargeHistoryRepository();
            chargeHistoryRepository.InsertChargeResponseHistory(callbackResult);
        }
    }
}
